import java.util.Arrays;
import java.util.Scanner;
public class Main3 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] id=new String[n];
		String[] str = new String[n];
		String [] date = new String[n];
		for(int i =0;i<n;i++){
			id[i]=sc.nextLine();
		}
		for(int i=0;i<n;i++){
			str[i]=getBirthDate(id[i]);
			date[i]=id[i].substring(6, 14);
		}
		
		
		while(true){
			String choice =sc.nextLine();
			switch (choice){
				case "sort1":
					Arrays.sort(str);
					for(int i=0;i<n;i++)
					{
						System.out.println(str[i]);
					}
					break;
				case "sort2":
					for(int i=0;i<n;i++)
					{
						for(int j=0;j<n-1;j++)
						{
							String pre = date[j];
							int pre1 = Integer.parseInt(pre);
							String next = date[j+1];
							int next1 = Integer.parseInt(next);
							if(pre1>next1)
							{
								String temp = date[j];
								date[j] = date[j+1];
								date[j+1] = temp;
								String temp2 = id[j];
								id[j] = id[j+1];
								id[j+1] = temp2;
							}
						}
					}
					for(int i=0;i<n;i++)
						System.out.println(id[i]);
			break;
				default:
					System.out.println("exit");
					break;
		}

	}
	}
	private static String getBirthDate(String id) {
		String year = id.substring(6, 10);
		String month = id.substring(10, 12);
		String date = id.substring(12, 14);
	return year+"-"+month+"-"+date;
}
}
